{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "21cfea06-af98-496a-b13b-106c335a2e65",
   "metadata": {},
   "source": [
    "# Understand Events and their Effects\n",
    "\n",
    "Effects are useful to create conditional relationships in your data: if some events happen `Effect`s can be configured to modify vectors in order to reflect the induced change of the event."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1bf67328-efe5-4c88-9c36-ce2d4b20d89f",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install superlinked==37.5.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "11664035-fff3-4c38-97f3-f2fbb0d46778",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime, timedelta\n",
    "import pandas as pd\n",
    "from superlinked import framework as sl\n",
    "\n",
    "pd.set_option(\"display.max_colwidth\", 100)\n",
    "\n",
    "# set \"NOW\" for a fixed data so the notebook runs the same regardless of the date\n",
    "date_time_obj = datetime(year=2024, month=8, day=7, hour=0, minute=0, second=0)\n",
    "now_timestamp = int(date_time_obj.timestamp())\n",
    "EXECUTOR_DATA = {sl.CONTEXT_COMMON: {sl.CONTEXT_COMMON_NOW: now_timestamp}}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70951ddc-ef9d-44af-a984-bebe69ca70e6",
   "metadata": {},
   "source": [
    "## Setting up event schemas\n",
    "\n",
    "Events generally have:\n",
    "- `SchemaReference`s: these contain ids that are resolved in the referenced schema. These reflect the items which were constituents of the event.\n",
    "- event_type as a string: used to group events so that `Effect`s can be applied to a subset of events\n",
    "- and id of course"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2df24eaf-f9b8-404b-8b7f-0a9d2c2284df",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Paragraph(sl.Schema):\n",
    "    id: sl.IdField\n",
    "    body: sl.String\n",
    "\n",
    "\n",
    "class User(sl.Schema):\n",
    "    id: sl.IdField\n",
    "    interest: sl.String\n",
    "\n",
    "\n",
    "class Event(sl.EventSchema):\n",
    "    id: sl.IdField\n",
    "    created_at: sl.CreatedAtField\n",
    "    paragraph: sl.SchemaReference[Paragraph]\n",
    "    user: sl.SchemaReference[User]\n",
    "    event_type: sl.String\n",
    "\n",
    "\n",
    "paragraph = Paragraph()\n",
    "user = User()\n",
    "event = Event()\n",
    "\n",
    "relevance_space = sl.TextSimilaritySpace(\n",
    "    text=[user.interest, paragraph.body],\n",
    "    model=\"sentence-transformers/all-mpnet-base-v2\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e860a56c-6e16-4b1d-a03d-1cde54f544a4",
   "metadata": {},
   "source": [
    "Effects are the way to set up how and to what extent vectors should affect each other conditional on some event."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0e71b755-53cc-4715-b561-cb354b577a39",
   "metadata": {},
   "outputs": [],
   "source": [
    "# weights in effects control importance between events\n",
    "# effectively doesn't matter if there is only one effect in the index.\n",
    "event_effects = [\n",
    "    sl.Effect(\n",
    "        relevance_space,\n",
    "        event.user,\n",
    "        0.8 * event.paragraph,\n",
    "        event.event_type == \"read\",\n",
    "    )\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b75a1771-2469-47d6-9e1c-3e1645c79eec",
   "metadata": {},
   "source": [
    "We are setting up multiple indexes to understand the differences in terms of the trade-off around `event_influence` - whether the initial vector of the entity, or the event effects should matter more."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9243d48f-1b32-4262-b499-a5e58be6f66b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# for this index, only initial data of the user will matter as event_influence is 0.\n",
    "index_low_event_infl = sl.Index(spaces=relevance_space, effects=event_effects, event_influence=0.0)\n",
    "# for this index, initial data and events of the user will matter equally as event_influence is 0.5.\n",
    "index_mid_event_infl = sl.Index(spaces=relevance_space, effects=event_effects, event_influence=0.5)\n",
    "# high event_influence means the emphasis shifts to event data, and the initial vector will matter less.\n",
    "# Eventually the initial vector will not matter when `event_influence = 1.0`.\n",
    "index_high_event_infl = sl.Index(spaces=relevance_space, effects=event_effects, event_influence=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8530161-14aa-4076-85f3-f9b5acdad377",
   "metadata": {},
   "source": [
    "Let's also setup different indexes to understand the `temperature` parameter. It is a way to add recency bias to the system in terms of event registration order. It is a way to bias towards freshly ingested events.\n",
    "- `temperature > 0.5` biases towards newer items. Setting it to `1.0` results in the latest event overwriting the aggregate event effects accumulated to that point.\n",
    "- `temperature < 0.5` create bias towards older items and the vector is less sensitive to changes due to new events. Setting it to `0.0` doesn't really make sense as it will keep the event aggregate affect non-existent."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "32a54de8-5bf2-47da-8ce8-95725fb8aaef",
   "metadata": {},
   "outputs": [],
   "source": [
    "index_low_temp = sl.Index(spaces=relevance_space, effects=event_effects, temperature=0.25)\n",
    "\n",
    "index_high_temp = sl.Index(spaces=relevance_space, effects=event_effects, temperature=0.75)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3655ac4d-8960-41ae-b912-5099c3576d29",
   "metadata": {},
   "source": [
    "**_NOTE 1:_**  `event_influence` can be any number between 0.0 and 1.0 and controls the tradeoff between initial entity vectors and event effects. Its value can be set based on business logic or parameter tuning. `0.5` is a sensible default balancing between the two."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d9aaa36-4f4e-414c-9fb9-4eec883cea2e",
   "metadata": {},
   "source": [
    "**_NOTE 2:_**  `temperature` can be any number between 0.0 and 1.0 and controls the aggregation of previously aggregated event effects and the current event effect when aggregating the two. `0.5` is a sensible default creating an equal balance."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ff6934a-47a4-49cd-9db5-2817f9f58492",
   "metadata": {},
   "source": [
    "**_NOTE 3:_**  `Index` argument `max_age` defaults to `None` if omitted meaning no restriction. If set, events older than it will be filtered out and will not affect the vector. Only takes effect in the batch system."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "674e70c2-82a2-416c-b718-e52906b996ba",
   "metadata": {},
   "source": [
    "**_NOTE 4:_**  `Index` argument `max_count` defaults to `None` if omitted meaning no restriction. If set, only the last `max_count` events are considered. Only takes effect in our batch system."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "663d35df-a4b6-4dd0-914e-df8f45ac6d88",
   "metadata": {},
   "source": [
    "**_NOTE 5:_**  `Index` argument `time_decay_floor` defaults to `1.0` if omitted meaning the time-based modifier of event weights are all equally `1.0`. As a result, timestamps stored in the `CreatedAtField`s of events do not take effect. Set it to a number closer to `0.0` to achieve decaying weights for older events."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73077b27-954b-4260-bcba-4a17bad4a75e",
   "metadata": {},
   "source": [
    "### Superlinked setup\n",
    "\n",
    "Now let's set our local superlinked system up and ingest data of a user, 3 documents and an event where the user read the second doument. The user originally expressed interest in wild animals. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4aed826a-470e-4e53-8f2a-e8e529373b61",
   "metadata": {},
   "outputs": [],
   "source": [
    "source_paragraph: sl.InMemorySource = sl.InMemorySource(paragraph)\n",
    "source_user: sl.InMemorySource = sl.InMemorySource(user)\n",
    "source_event: sl.InMemorySource = sl.InMemorySource(event)\n",
    "executor = sl.InMemoryExecutor(\n",
    "    sources=[source_paragraph, source_user, source_event],\n",
    "    indices=[\n",
    "        index_low_event_infl,\n",
    "        index_mid_event_infl,\n",
    "        index_high_event_infl,\n",
    "        index_low_temp,\n",
    "        index_high_temp,\n",
    "    ],\n",
    ")\n",
    "app = executor.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "61d76e31-72f4-4404-8cc1-31c08d08d978",
   "metadata": {},
   "outputs": [],
   "source": [
    "source_paragraph.put(\n",
    "    [\n",
    "        {\"id\": \"paragraph-1\", \"body\": \"Glorious animals live in the wilderness.\"},\n",
    "        {\n",
    "            \"id\": \"paragraph-2\",\n",
    "            \"body\": \"Growing computation power enables advancements in AI.\",\n",
    "        },\n",
    "        {\n",
    "            \"id\": \"paragraph-3\",\n",
    "            \"body\": \"Stock markets are reaching all time highs during 2024.\",\n",
    "        },\n",
    "    ]\n",
    ")\n",
    "\n",
    "source_user.put([{\"id\": \"user-1\", \"interest\": \"I am interested in wild animals.\"}])\n",
    "\n",
    "source_event.put(\n",
    "    [\n",
    "        {\n",
    "            \"id\": \"event-1\",\n",
    "            \"created_at\": int((date_time_obj - timedelta(days=2)).timestamp()),  # 2 days old event\n",
    "            \"paragraph\": \"paragraph-2\",\n",
    "            \"user\": \"user-1\",\n",
    "            \"event_type\": \"read\",\n",
    "        }\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32fdafca-ccee-4760-a7dc-d8cff993df9b",
   "metadata": {},
   "source": [
    "The creation time of events matter in many use-cases - more recent events are generally more important. The time related modifier of event weights is linearly correlated with the creation time (`created_at` field) of the event and it's relative position between `NOW` and `NOW - max_age`. Set `time_decay_floor` to value closer to `0.0` to increase that effect - a default `1.0` value means timestamps of events are not taken into account. Nevertheless, setting `0.5 < temperature <= 1` will create a recency bias as indicated above. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5572403d-b848-4da7-9c8c-7655abd0bc1e",
   "metadata": {},
   "source": [
    "## Making the initial vector count more\n",
    "\n",
    "Setting `event_influence` to 0 in `index_low_event_infl`, the fact the user read a different paragraph (about AI) does not matter - the initial interest in wild animals will prevail. The index is unaffected by the event."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "aaa75750-3b1c-4d68-af9c-133a249f6b0b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>body</th>\n",
       "      <th>id</th>\n",
       "      <th>similarity_score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Glorious animals live in the wilderness.</td>\n",
       "      <td>paragraph-1</td>\n",
       "      <td>0.490420</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Stock markets are reaching all time highs during 2024.</td>\n",
       "      <td>paragraph-3</td>\n",
       "      <td>0.045257</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Growing computation power enables advancements in AI.</td>\n",
       "      <td>paragraph-2</td>\n",
       "      <td>0.020563</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                     body           id  \\\n",
       "0                Glorious animals live in the wilderness.  paragraph-1   \n",
       "1  Stock markets are reaching all time highs during 2024.  paragraph-3   \n",
       "2   Growing computation power enables advancements in AI.  paragraph-2   \n",
       "\n",
       "   similarity_score  rank  \n",
       "0          0.490420     0  \n",
       "1          0.045257     1  \n",
       "2          0.020563     2  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_low_event_infl = (\n",
    "    sl.Query(index_low_event_infl).find(paragraph).with_vector(user, sl.Param(\"user_id\")).select_all()\n",
    ")\n",
    "\n",
    "result_low_event_infl = app.query(\n",
    "    query_low_event_infl,\n",
    "    user_id=\"user-1\",\n",
    ")\n",
    "\n",
    "sl.PandasConverter.to_pandas(result_low_event_infl)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66e9ade3-0f54-4a0c-88e8-85641b6a140f",
   "metadata": {},
   "source": [
    "## The power of events\n",
    "\n",
    "Increasing event_influence switches the effect of events on, and shifts the user vector away from the initial interest (wild animals) towards newly read topics (AI). Even though the user expressed interest in wild animals, as an other document about AI was read the preferences shifted towards the actual empirical assessment of their interest. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "efce4bfc-e21e-4f06-97e3-d33e07da228c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>body</th>\n",
       "      <th>id</th>\n",
       "      <th>similarity_score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Growing computation power enables advancements in AI.</td>\n",
       "      <td>paragraph-2</td>\n",
       "      <td>0.634420</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Glorious animals live in the wilderness.</td>\n",
       "      <td>paragraph-1</td>\n",
       "      <td>0.401294</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Stock markets are reaching all time highs during 2024.</td>\n",
       "      <td>paragraph-3</td>\n",
       "      <td>0.187389</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                     body           id  \\\n",
       "0   Growing computation power enables advancements in AI.  paragraph-2   \n",
       "1                Glorious animals live in the wilderness.  paragraph-1   \n",
       "2  Stock markets are reaching all time highs during 2024.  paragraph-3   \n",
       "\n",
       "   similarity_score  rank  \n",
       "0          0.634420     0  \n",
       "1          0.401294     1  \n",
       "2          0.187389     2  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_mid_event_infl = (\n",
    "    sl.Query(index_mid_event_infl).find(paragraph).with_vector(user, sl.Param(\"user_id\")).select_all()\n",
    ")\n",
    "\n",
    "result_mid_event_infl = app.query(\n",
    "    query_mid_event_infl,\n",
    "    user_id=\"user-1\",\n",
    ")\n",
    "\n",
    "sl.PandasConverter.to_pandas(result_mid_event_infl)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36b747ae-845d-42ac-bf36-19358b63d4f7",
   "metadata": {},
   "source": [
    "### Driven fully by events\n",
    "\n",
    "Setting `event_influence` to 1 means similarities are driven entirely by the event data: hence the `1.0` similarity to the read paragraph."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "108be352-ec0b-4764-8523-05ab36723752",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>body</th>\n",
       "      <th>id</th>\n",
       "      <th>similarity_score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Growing computation power enables advancements in AI.</td>\n",
       "      <td>paragraph-2</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Stock markets are reaching all time highs during 2024.</td>\n",
       "      <td>paragraph-3</td>\n",
       "      <td>0.246391</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Glorious animals live in the wilderness.</td>\n",
       "      <td>paragraph-1</td>\n",
       "      <td>0.035769</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                     body           id  \\\n",
       "0   Growing computation power enables advancements in AI.  paragraph-2   \n",
       "1  Stock markets are reaching all time highs during 2024.  paragraph-3   \n",
       "2                Glorious animals live in the wilderness.  paragraph-1   \n",
       "\n",
       "   similarity_score  rank  \n",
       "0          1.000000     0  \n",
       "1          0.246391     1  \n",
       "2          0.035769     2  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_high_event_infl = (\n",
    "    sl.Query(index_high_event_infl).find(paragraph).with_vector(user, sl.Param(\"user_id\")).select_all()\n",
    ")\n",
    "\n",
    "result_high_event_infl = app.query(\n",
    "    query_high_event_infl,\n",
    "    user_id=\"user-1\",\n",
    ")\n",
    "\n",
    "sl.PandasConverter.to_pandas(result_high_event_infl)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efd46432-b79f-43df-974f-1598c160194d",
   "metadata": {},
   "source": [
    "### The effect of temperature\n",
    "\n",
    "Now let's ingest a second event - where our user read the 3rd `paragraph` about stock markets. The initial interest in wild animals (therefore in the first `paragraph`), and the reading of the second `paragraph` about AI are uniform in these 2 following cases. However, we can observe how the different values of temperature move the user vector closer to the 3rd `paragraph` on stock markets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "11b42812-e3b3-4b52-8d5f-d65cebd23124",
   "metadata": {},
   "outputs": [],
   "source": [
    "source_event.put(\n",
    "    [\n",
    "        {\n",
    "            \"id\": \"event-2\",\n",
    "            \"created_at\": int((date_time_obj - timedelta(days=1)).timestamp()),  # 1 days old event\n",
    "            \"paragraph\": \"paragraph-3\",\n",
    "            \"user\": \"user-1\",\n",
    "            \"event_type\": \"read\",\n",
    "        }\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44975f4c-aad6-46f8-9639-ed15fa143637",
   "metadata": {},
   "source": [
    "Even a lower temperature moves the user vector much closer to the 3rd `paragraph`..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d43000d0-9b85-485d-b004-73f25a72fc8b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>body</th>\n",
       "      <th>id</th>\n",
       "      <th>similarity_score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Growing computation power enables advancements in AI.</td>\n",
       "      <td>paragraph-2</td>\n",
       "      <td>0.546575</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Glorious animals live in the wilderness.</td>\n",
       "      <td>paragraph-1</td>\n",
       "      <td>0.422580</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Stock markets are reaching all time highs during 2024.</td>\n",
       "      <td>paragraph-3</td>\n",
       "      <td>0.320754</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                     body           id  \\\n",
       "0   Growing computation power enables advancements in AI.  paragraph-2   \n",
       "1                Glorious animals live in the wilderness.  paragraph-1   \n",
       "2  Stock markets are reaching all time highs during 2024.  paragraph-3   \n",
       "\n",
       "   similarity_score  rank  \n",
       "0          0.546575     0  \n",
       "1          0.422580     1  \n",
       "2          0.320754     2  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_low_temp = sl.Query(index_low_temp).find(paragraph).with_vector(user, sl.Param(\"user_id\")).select_all()\n",
    "\n",
    "result_low_temp = app.query(\n",
    "    query_low_temp,\n",
    "    user_id=\"user-1\",\n",
    ")\n",
    "\n",
    "sl.PandasConverter.to_pandas(result_low_temp)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11a90026-ddd4-41e9-becb-5ba0a286dfd9",
   "metadata": {},
   "source": [
    "... but the higher temperature makes that `paragraph` the closest to the user's interest."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cb8ce81a-36d0-42c5-b3bb-273f8b143d34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>body</th>\n",
       "      <th>id</th>\n",
       "      <th>similarity_score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Stock markets are reaching all time highs during 2024.</td>\n",
       "      <td>paragraph-3</td>\n",
       "      <td>0.563034</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Glorious animals live in the wilderness.</td>\n",
       "      <td>paragraph-1</td>\n",
       "      <td>0.417958</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Growing computation power enables advancements in AI.</td>\n",
       "      <td>paragraph-2</td>\n",
       "      <td>0.298646</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                     body           id  \\\n",
       "0  Stock markets are reaching all time highs during 2024.  paragraph-3   \n",
       "1                Glorious animals live in the wilderness.  paragraph-1   \n",
       "2   Growing computation power enables advancements in AI.  paragraph-2   \n",
       "\n",
       "   similarity_score  rank  \n",
       "0          0.563034     0  \n",
       "1          0.417958     1  \n",
       "2          0.298646     2  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_high_temp = sl.Query(index_high_temp).find(paragraph).with_vector(user, sl.Param(\"user_id\")).select_all()\n",
    "\n",
    "result_high_temp = app.query(\n",
    "    query_high_temp,\n",
    "    user_id=\"user-1\",\n",
    ")\n",
    "\n",
    "sl.PandasConverter.to_pandas(result_high_temp)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
